<template>
  <Table ref="tableRef" :columns="columns" :data="loadData" :alert="false" bordered>
    <template #bodyCell="{ column, record, index }">
      <template v-if="column.dataIndex === 'index'"> {{ index + 1 }} </template>
      <template v-if="column.dataIndex === 'password'">
        <div class="table-password">
          <span class="table-password-text">{{ record.showPassword ? record.password : '********' }}</span>
          <span class="table-password-icon" @click="record.showPassword = !record.showPassword">
            <SvgIcon
              v-if="record.showPassword"
              class="iconDef"
              :name="'eyes'"
              :color="'#1890ff'"
              width="16"
              height="16"
            />
            <SvgIcon v-else class="iconDef" :name="'eyesClose'" :color="'#1890ff'" width="16" height="16" />
          </span>
        </div>
      </template>
      <template v-if="column.dataIndex === 'action'">
        <div class="table-operation">
          <a @click="handelDetial(record)">详情</a>
        </div>
      </template>
    </template>
  </Table>

  <BasicModal
    ref="showActionModal"
    :title="`${ModalName[type]}持续集成工具`"
    detilaTitle="详情-持续集成工具"
    width="500px"
    @emit-callback-fn="modalCallbackFn($event)"
  >
    <template #content> <SchemaForm ref="formRef" /></template>
  </BasicModal>
</template>

<script setup lang="ts">
import {} from '@ant-design/icons-vue';
import { getToolChainList } from '@/api/operationManagement';
import { useForm } from '@/components/SchemaForm/index';
import type { FormSchema } from '@/components/SchemaForm';
import { ModalName, ModalType } from '@/enums/commonEnum';
import * as CryptoUtils from '@/utils/cryptoUtils';
const columns = [
  { dataIndex: 'index', title: '序号', align: 'center', width: 60 },
  { dataIndex: 'code', title: '编码', align: 'center', ellipsis: true, width: 260 },
  { dataIndex: 'version', title: '工具版本', align: 'center', ellipsis: true },
  { dataIndex: 'server', title: '服务地址', align: 'center', ellipsis: true },
  { dataIndex: 'username', title: '用户名', align: 'center', ellipsis: true },
  { dataIndex: 'password', title: '密码', align: 'center', ellipsis: true },
  { dataIndex: 'creator', title: '创建人', align: 'center', ellipsis: true },
  { dataIndex: 'createTime', title: '创建时间', align: 'center', ellipsis: true },
  {
    title: '操作',
    dataIndex: 'action',
    align: 'center',
    fixed: 'right',
    width: 160,
  },
];

const type = ref<ModalType>(ModalType.add);
const tableRef = ref<any>('');
const showActionModal = ref<any>(null);

const [SchemaForm, formRef] = useForm({
  disabled: true,
  schemas: [
    {
      field: 'code',
      label: '编码',
      labelWidth: 100,
      colProps: {
        span: 22,
      },
      component: 'Input',
    },
    {
      field: 'version',
      label: '工具版本',
      labelWidth: 100,
      colProps: {
        span: 22,
      },
      component: 'Input',
    },
    {
      field: 'server',
      label: '服务地址',
      labelWidth: 100,
      colProps: {
        span: 22,
      },
      component: 'Input',
    },
    {
      field: 'username',
      label: '用户名',
      labelWidth: 100,
      colProps: {
        span: 22,
      },
      component: 'Input',
    },
    {
      field: 'password',
      label: '密码',
      labelWidth: 100,
      colProps: {
        span: 22,
      },
      component: 'ActionPassWord',
    },
    {
      field: 'creator',
      label: '创建人',
      labelWidth: 100,
      colProps: {
        span: 22,
      },
      component: 'Input',
    },
    {
      field: 'createTime',
      label: '创建时间',
      labelWidth: 100,
      colProps: {
        span: 22,
      },
      component: 'Input',
    },
  ] as FormSchema[],
});

const loadData = async (parameter: any) => {
  const parameObj = {
    currentPage: parameter.current,
    pageSize: parameter.size,
    condition: {
      type: 1,
    },
  };
  const data = await getList(parameObj);
  return data;
};

//获取表格数据
const getList = async (parameObj: any) => {
  return await getToolChainList(parameObj).then((res) => {
    const data = {
      total: res.totalCount,
      current: res.currentPage,
      pageSize: res.pageSize,
      records:
        res?.records.map((item: any) => {
          return {
            showPassword: false,
            ...item,
            password: CryptoUtils.decryptByRSA(item.password),
          };
        }) || [],
    };
    return data;
  });
};

//表格详情
const handelDetial = async (record: any) => {
  type.value = ModalType.detail;
  nextTick(() => {
    formRef.setFieldsValue(record);
  });
  showActionModal.value.showModal(record, '关闭');
};

const modalCallbackFn = async (obj: any) => {};
</script>

<style scoped lang="less">
.table-password {
  display: flex;
  align-items: center;
  position: relative;
}

.table-password-text {
  margin-right: 5px;
  display: inline-block;
  width: 100%;
}

.table-password-icon {
  cursor: pointer;
  position: absolute;
  right: 5px;
  top: 50%;
  transform: translate(0, -50%);
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
